package com.alipay.app.groupmeal.standard.ui.b.function.amount;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alipay.app.groupmeal.standard.R;
import com.alipay.app.groupmeal.standard.widget.InputAmountView;
import com.alipay.app.groupmeal.standard.widget.business.BaseBScreenView;

/**
 * 待机页面输入金额
 */
public class BScreenInputAmount extends BaseBScreenView {

    private InputAmountView tv_amount;
    private TextView tv_title;
    private LinearLayout ll_footer;

    private static final int T_DEFAULT = 0;//默认等待输入
    private static final int T_INPUT = 1;//正在输入

    private int mode = T_DEFAULT;//页面模式
    private boolean clearAmount;

    public BScreenInputAmount(Context context) {
        super(context);
    }

    public BScreenInputAmount(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_input_amount;
    }

    @Override
    protected void initView(View view) {
        tv_amount = findViewById(R.id.tv_amount);
        tv_title = findViewById(R.id.tv_title);
        ll_footer = findViewById(R.id.ll_footer);

        tv_amount.addTextChangedListener(new InputAmountView.CustomTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s) || count == 0) {
                    //等待输入
                    mode = T_DEFAULT;
                    ll_footer.setVisibility(View.VISIBLE);
                } else {
                    //输入中
                    mode = T_INPUT;
                    ll_footer.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void initData() {
        tv_title.setText(getContext().getString(R.string.txt_input_amount));
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            return false;
        } else {
            return tv_amount.dispatchKeyEvent(event);
        }

    }

    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (clearAmount) {
            //每次显示，页面恢复初始化
            tv_amount.clear();
        }
    }

    public CharSequence getAmount() {
        return tv_amount.getTextBySum();
    }

    public void setClearAmount(boolean clearAmount) {
        this.clearAmount = clearAmount;
    }

    public void setClearAmountForce() {
        this.clearAmount = true;
        if (tv_amount != null)
            tv_amount.clear();
    }
}
